package com.ytd.common.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Your class notes
 *
 * @author gaoguanchao
 * @since 2023/3/24
 */
public class FormatUtil {
    public static final SimpleDateFormat dayFormat = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
    public static final SimpleDateFormat dayFormat2 = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    private static final SimpleDateFormat logTimeFormat =
        new SimpleDateFormat("HH:mm:ss:SSS", Locale.CHINA);

    private static final SimpleDateFormat DATE_FORMAT =
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

    private static final SimpleDateFormat DATE_TIME_FORMAT =
        new SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.CHINA);

    private static final SimpleDateFormat MEAL_TIME_FORMAT =
        new SimpleDateFormat("HH:mm", Locale.CHINA);

    private static final SimpleDateFormat showTimeFormat =
        new SimpleDateFormat("HH:mm:ss", Locale.CHINA);

    public static String getDayFormat(Date date) {
        return dayFormat.format(date);
    }

    public static String getDayFormat2(Date date) {
        return dayFormat2.format(date);
    }

    public static String getLogTimeFormat(Date date) {
        return logTimeFormat.format(date);
    }

    public static String getDateFormat(Date date) {
        return DATE_FORMAT.format(date);
    }

    public static String getDateTimeFormat(Date date) {
        return DATE_TIME_FORMAT.format(date);
    }

    public static String getShowDateFormat() {
        return showTimeFormat.format(new Date());
    }

    public static long getFormatDate(String dateFormat) {
        try {
            Date parse = DATE_FORMAT.parse(dateFormat);
            if (parse != null) {
                return parse.getTime();
            }
        } catch (ParseException e) {
            LogUtil.e("getFormatDate ParseException ", e);
        }
        return 0;
    }

    public static String getNowMealTimeString() {
        return getMealTime(new Date());
    }

    public static String getMealTime(Date date) {
        return MEAL_TIME_FORMAT.format(date);
    }

    public static String getSecurityAccount(String account) {
        return account.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
    }

    public static Date parseMealTime(String time) {
        try {
            return MEAL_TIME_FORMAT.parse(time);
        } catch (ParseException e) {
        }
        return new Date();
    }


    public static int parseInt(String str) {
        try {
            return Integer.parseInt(str);
        } catch (Throwable e) {
            return 0;
        }
    }

    public static Long parseLong(String str) {
        try {
            return Long.parseLong(str);
        } catch (Throwable e) {
            return 0L;
        }
    }

    public static String getSecurityNickName(String nickName) {
        if (TextUtils.isEmpty(nickName)) {
            return "";
        }
        if (nickName.length() < 2) {
            return nickName;
        }
        if (nickName.length() == 2) {
            return nickName.charAt(0) + "*";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(nickName.charAt(0));
        for (int i = 1; i < nickName.length() - 1; i++) {
            sb.append("*");
        }
        sb.append(nickName.substring(nickName.length() - 1));
        return sb.toString();
    }

    public static Double parseDouble(String price) {
        try {
            return Double.parseDouble(price);
        } catch (Throwable e) {
            return 0d;
        }
    }
}
